<?php
namespace backend\models;
use Yii;
use \yii\db\ActiveRecord;

class Admin extends ActiveRecord {
    public $verifyCode;
    public $remember;
    public $repass;
    public $uid;

    public static function tableName(){
        return '{{%admin}}';
    }

    public function rules(){
        return [
            [['username', 'password'], 'trim' , 'on' => 'login'],
            ['username', 'required', 'message' => '账号不能为空', 'on' => ['login', 'seekpass', 'addadmin']],
            ['password', 'required', 'message' => '密码不能为空', 'on' => ['login', 'changepass', 'addadmin']],
            ['username', 'string', 'length' => [2, 10], 'message' => '长度不能小于2大于10','on' => 'login'],
            ['password', 'string', 'length' => [2, 10], 'message' => '长度不能小于2大于10', 'on' => ['login', 'changepass']],
            ['password', 'validatePasword', 'on' => 'login'],
            ['verifyCode', 'captcha', 'message' => '验证码错误', 'captchaAction'=>'public/captcha', 'on' => 'login'],

            ['useremail', 'required', 'message' => '电子邮箱不能为空', 'on' => ['seekpass', 'addadmin']],
            ['useremail', 'email', 'message' => '电子邮箱格式不正确', 'on' => ['seekpass', 'addadmin']],
            ['useremail', 'validateEmail', 'on' => 'seekpass'],

            ['repass', 'required', 'message' => '重复密码不能为空', 'on' => ['changepass', 'addadmin']],
            ['repass', 'compare', 'compareAttribute' => 'password', 'message' => '两次输入的密码不一致' ,'on' => ['changepass','addadmin']],
        ];
    }

    public function attributeLabels() {
        return [
          'username' => '用户名',
          'useremail'=> '邮箱',
          'password' => '密码',
          'repass' => '重复密码'
        ];
    }

    public function scenarios(){
        $scenarios = parent::scenarios();
        $scenarios['login'] = ['username', 'password', 'verifyCode'];
        $scenarios['seekpass'] = ['username', 'useremail'];
        $scenarios['changepass']  = ['password', 'repass'];
        $scenarios['addadmin'] = ['username', 'useremail', 'password', 'repass'];
        return $scenarios;
    }

    public function validatePasword(){
       if(!$this->hasErrors()){
           $data = self::find()->where('username = :user and password = :pass', [':user' => $this->username, ':pass' => md5($this->password)])->one();
           if(is_null($data)){
               $this->addError('username', '用户名或密码错误');
           }
       }
    }

    public function validateEmail(){
        if(!$this->hasErrors()){
            $data = self::find()->where('username = :user and useremail = :email', [':user' => $this->username, ':email' => $this->useremail])->one();
            if(is_null($data)){
                $this->addError('useremail', '账号电子邮箱不正确');
            }
        }
    }

    public function login($data){
        if($this->load($data) && $this->validate()){
            $lifetime = $this->remember ? 24*3600 : 0;
            $session = Yii::$app->session;
            session_set_cookie_params($lifetime);
            $session['admin'] = [
                'username' => $this->username,
                'isLogin' => 1,
            ];
            $this->updateAll(['login_time' => time(), 'login_ip' => ip2long(Yii::$app->request->userIP)], 'username = :user', [':user' => $this->username]);
            return (bool)$session['admin']['isLogin'];
        }
        return false;
    }

    public function addAdmin($data){
        if($this->load($data) && $this->validate()){
            $this->username  = $data['Admin']['username'];
            $this->useremail = $data['Admin']['useremail'];
            $this->password  = md5($data['Admin']['password']);
            $this->login_time  = time();
            $this->create_time = time();
            $this->login_ip    = ip2long(Yii::$app->request->userIP);
            $this->save();
            return 2222;
        }
        return 1111;
    }

    public function seekpass($data){
        if($this->load($data)  && $this->validate()){
            $time = time();
            $token = $this->createToken($data['Admin']['username'], $time);
            $mailer = Yii::$app->mailer->compose('seekpass', ['username' => $data['Admin']['username'], 'time' => $time, 'token' => $token]);
            $mailer->setFrom('13611861503@163.com');
            $mailer->setTo($data['Admin']['useremail']);
            $mailer->setSubject('Cici博客-找回密码');
            if($mailer->send()){
                return true;
            }
        }
        return false;
    }

    public function createToken($username, $time){
        return md5(md5($username) . base64_encode(Yii::$app->request->userIP) . md5($time));
    }

    public function changePass($data){
        if($this->load($data) && $this->validate()){
             return (bool) $this->updateAll(['password' => md5($data['Admin']['password'])], 'username = :user', [':user' => $data['Admin']['username']]);
        }
        return false;
    }
}































